//
//  SelectUserViewController.m
//  Rail
//
//  Created by hychen on 2018/3/4.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "SelectUserViewController.h"
#import "SelectUserTableViewCell.h"
#import "EVNCustomSearchBar.h"
#import "MemberInfoViewController.h"
#import "FaceDetailViewController.h"
#import "CustomerFaceGatherViewController.h"
#import "FaceGatherViewController.h"

@interface SelectUserViewController () <EVNCustomSearchBarDelegate>
@property (nonatomic, copy) NSString *searchText;
@property (nonatomic, strong) EVNCustomSearchBar *searchView;
@end

@implementation SelectUserViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:@"人脸采集"];
    
    [self setupRightNav];
    
    [self setupSearchView];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.searchView.mas_bottom);
    }];
    
    self.isOpenFooter = YES;
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupRightNav {
    UIButton *customer = [UIButton buttonWithTitle:@"访客采集" titleColor:kWhiteColor norImage:nil highImage:nil action:@selector(gotoCustomerVc) owner:self];
    customer.titleLabel.font = [UIFont systemFontOfSize:14.f];
    customer.frame = CGRectMake(0, 0, 60, 40);
    UIBarButtonItem *customerItem = [[UIBarButtonItem alloc] initWithCustomView:customer];
    self.navigationItem.rightBarButtonItem = customerItem;
}

- (void)gotoCustomerVc {
    CustomerFaceGatherViewController *vc = [CustomerFaceGatherViewController new];
    BMWeakSelf
    [vc setBlockGatherCustomerSuccess:^{
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setupSearchView {
    self.searchView = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 44)];
    self.searchView.backgroundColor = Color_NavBgColor;
    self.searchView.iconAlign = EVNCustomSearchBarIconAlignCenter;
    self.searchView.iconImage = [UIImage imageNamed:@"EVNCustomSearchBar.bundle/searchImageBlack"];
    self.searchView.placeholder = @"搜索";
    self.searchView.delegate = self;
    self.searchView.isHiddenCancelButton = NO;
    [self.view addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(45);
    }];
}

- (void)refreshData {
    self.currentPage = 1;
    self.isOpenFooter = NO;
    BMWeakSelf
    [HttpRequest loadGatherFaceUserListDataWithPageNo:[@(self.currentPage) stringValue] keywork:self.searchText?:@"" success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]]) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:object];
            [weakSelf.tableView.mj_footer resetNoMoreData];
            if (object.count > 9) {
                weakSelf.isOpenFooter = YES;
            }
        }
        else {
            weakSelf.dataArray = [NSMutableArray array];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView reloadData];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (void)loadMoreData {
    self.currentPage++;
    BMWeakSelf
    [HttpRequest loadGatherFaceUserListDataWithPageNo:[@(self.currentPage) stringValue] keywork:self.searchText?:@"" success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            [weakSelf.dataArray addObjectsFromArray:object];
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView reloadData];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectUserInfoModel *model = [self.dataArray objectAtIndex:indexPath.row];
    SelectUserTableViewCell *cell = [SelectUserTableViewCell cellWithTableView:tableView class:[SelectUserTableViewCell class]];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SelectUserInfoModel *model = [self.dataArray objectAtIndex:indexPath.row];

    if (![model isKindOfClass:[SelectUserInfoModel class]]) {
        Show_SV_Error_Msg(@"数据出错,请重新刷新数据")
        return;
    }
    
    if ([model.seqid isKindOfClass:[NSString class]] && model.seqid.length > 0 && ![model.seqid isEqualToString:@"0"]) {
        FaceDetailViewController *vc = [FaceDetailViewController new];
        vc.userModel = model;
        BMWeakSelf
        [vc setBlockRegatherSuccess:^{
            [weakSelf.tableView.mj_header beginRefreshing];
        }];
        [vc setBlockDeleteSuccess:^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.tableView.mj_header beginRefreshing];
            });
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else {
        BMWeakSelf
        FaceGatherViewController *vc = [FaceGatherViewController new];
        [vc setBlockGatherFaceSuccess:^{
            [weakSelf.tableView.mj_header beginRefreshing];
        }];
        vc.userModel = model;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

- (void)searchBarTextDidEndEditing:(EVNCustomSearchBar *)searchBar {
    self.searchText = searchBar.text;
    [self.tableView.mj_header beginRefreshing];
}

- (void)searchBarSearchButtonClicked:(EVNCustomSearchBar *)searchBar {
    self.searchText = searchBar.text;
    [self.tableView.mj_header beginRefreshing];
}

- (void)searchBar:(EVNCustomSearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ([searchText isEqualToString:@""]) {
        self.searchText = searchText;
        [self refreshData];
    }
    
}

- (void)searchBarCancelButtonClicked:(EVNCustomSearchBar *)searchBar {
    [self.searchView resignFirstResponder];
}
@end
